package cn.bdqn.controller;

import cn.bdqn.bean.result.Page;
import cn.bdqn.bean.result.Result;
import cn.bdqn.bean.result.ResultGenerator;
import cn.bdqn.domain.Damage;
import cn.bdqn.service.DamageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/damage")
public class DamageController {

    @Autowired
    private DamageService damageService;

    //分页  查询所有的报损记录
    @GetMapping("/queryAll")
    public Result queryAll(){
        Page<Damage> damageList=damageService.queryAll(1);
        if(damageList != null){
            return ResultGenerator.genSuccessResult(damageList);
        }else {
            return ResultGenerator.genFailResult("未查询到数据");
        }
    }

    //根据条件查询
    @GetMapping("/querySelective")
    public Result querySelective(Damage damage){
        Page<Damage> damageList=damageService.querySelective(damage,1);
        if(damageList != null){
            return ResultGenerator.genSuccessResult(damageList);
        }else {
            return ResultGenerator.genFailResult("未查询到数据");
        }
    }

    //分页  根据类型查询
    @GetMapping("/queryByType")
    public Result queryByType(Integer type){
        Page<Damage> damageList=damageService.queryByType(type,1);
        if(damageList != null){
            return ResultGenerator.genSuccessResult(damageList);
        }else {
            return ResultGenerator.genFailResult("未查询到数据");
        }
    }

    //分页  根据药品名查询
    @GetMapping("/queryByName")
    public Result queryByName(String damageName){
        Page<Damage> damageList=damageService.queryByName(damageName,1);
        if(damageList != null){
            return ResultGenerator.genSuccessResult(damageList);
        }else {
            return ResultGenerator.genFailResult("未查询到数据");
        }
    }

    //分页   根据申请编号查询
    @GetMapping("/applyNo")
    public Result queryByApplyNo(String applyNo){
        Page<Damage> damageList=damageService.queryByApplyNo(applyNo,1);
        if(damageList != null){
            return ResultGenerator.genSuccessResult(damageList);
        }else {
            return ResultGenerator.genFailResult("未查询到数据");
        }
    }

    //添加一条报损记录
    @PostMapping("/addDamage")
    public Result addDamage(Damage damage){
        try{
            damageService.addDamage(damage);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("发生了错误，添加失败");
        }
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/changeById")
    public Result changeById(Damage damage){
        try{
            damageService.changeById(damage);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("发生了错误，修改失败");
        }
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/removeById")
    public Result removeById(Integer id){
        try{
            damageService.removeById(id);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("发生了错误，删除失败");
        }
        return ResultGenerator.genSuccessResult();
    }


}
